package Thread2;

public class Demo14 {
    private static int count = 0;
    public static void main(String[] args) throws InterruptedException {
        Object locker = new Object();
        //String locker = new String();
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                synchronized (locker){
                    count++;
                }
            }
            System.out.println("t1 结束");
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                synchronized (locker){
                    count++;
                }
            }
            System.out.println("t2 结束");
        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        // 一个线程自增5w 次 两个线程应该自增10w 次,预期结果 count = 100000
        System.out.println(count);
    }
}
